package example.writable;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 *
 */
public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    private Text outKey = new Text();
    private FlowBean flowBean = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, FlowBean>.Context context)
            throws IOException, InterruptedException {

        String s = value.toString();
        String[] lines = s.split("\t");
        String phone = lines[1];
        String up = lines[lines.length - 3];
        String down = lines[lines.length - 2];

        outKey.set(phone);
        flowBean.setUpFlow(Long.valueOf(up));
        flowBean.setDownFlow(Long.valueOf(down));
        flowBean.setSumFlow(flowBean.getUpFlow() + flowBean.getDownFlow());

        context.write(outKey, flowBean);
    }
}
